package com.chinaisafe.user.biz.impl;

import cn.hutool.core.lang.Assert;
import com.chinaisafe.base.common.exception.BizException;
import com.chinaisafe.common.sdk.constant.UserConstant;
import com.chinaisafe.common.sdk.enums.AppTypeEnum;
import com.chinaisafe.user.biz.SysTenantAppBiz;
import com.chinaisafe.user.entity.SysApp;
import com.chinaisafe.user.entity.SysTenantApp;
import com.chinaisafe.user.service.SysAppService;
import com.chinaisafe.user.service.SysTenantAppService;
import com.chinaisafe.user.vo.request.SysTenantAppReqVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * SysTenantAppBizImpl
 * Description：系统级应用管理
 *
 * @author tby
 * @date 2024/7/13 15:59
 */
@Service
public class SysTenantAppBizImpl implements SysTenantAppBiz {

    @Resource
    private SysTenantAppService sysTenantAppService;

    private SysAppService sysAppService;

    @Override
    public void shelfSwitch(Long sysAppId) {
        SysTenantApp sysTenantApp = sysTenantAppService.lambdaQuery().eq(SysTenantApp::getSysAppId, sysAppId).one();
        if (sysTenantApp == null) {
            throw BizException.build("对不起,您还未获得该应用授权,请联系管理员");
        } else {
            boolean shelf = !sysTenantApp.getShelf();
            if (shelf && !sysTenantApp.getIntegration()) {
                SysApp sysApp = sysAppService.getById(sysAppId);
                if (sysApp.getType().equals(AppTypeEnum.OUTER.name())) {
                    throw BizException.build("对不起,该应用为外部应用,请先进行集成");
                }
            }
            sysTenantAppService.lambdaUpdate().set(SysTenantApp::getShelf, shelf).eq(SysTenantApp::getId, sysTenantApp.getId()).update();
        }
    }

    @Override
    public void rename(SysTenantAppReqVO sysTenantAppReqVO) {

        sysTenantAppService.lambdaUpdate().set(SysTenantApp::getAlias, sysTenantAppReqVO.getAlias()).eq(SysTenantApp::getId, sysTenantAppReqVO.getId()).update();
    }
}
